package cz.vse.javaee.album;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Bean pro stránku upload.jsp. Obaluje {@link cz.vse.javaee.album.AlbumDAO}
 * a udržuje odkaz na bean otevřeného detailu fotografie. Instance je ukládána
 * do session. Současně implementuje <code>HttpSessionBindingListener</code>
 * kvůli čistícím akcím při ukončování http session. 
 */
public class AlbumPageBean implements HttpSessionBindingListener {

    private AlbumDAO albumDAO;
    private PhotoPaginator paginator;
    private List<PhotoWithAlbumBasicTO> photos;
    private PhotoDetailPageBean detailBean;
    private Exporter exporter;

    public AlbumPageBean(PhotoPaginator paginator, AlbumDAO albumDAO, Exporter exporter) {
        this.paginator = paginator;
        this.albumDAO = albumDAO;
        this.exporter = exporter;
        next();
    }

    public AlbumDAO getAlbumDAO() {
        return albumDAO;
    }

    public void first() {
        photos = paginator.first();
    }

    public void last() {
        photos = paginator.last();
    }

    public final void next() {
        if (paginator.hasNext()) {
            photos = paginator.next();
        }
    }

    public void previous() {
        if (paginator.hasPrevious()) {
            photos = paginator.previous();
        }
    }

    public List<PhotoWithAlbumBasicTO> getPhotos() {
        return photos;
    }

    public PhotoPaginator getPaginator() {
        return paginator;
    }

    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
    }

    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        close();
    }

    public PhotoDetailPageBean getDetailBean() {
        return detailBean;
    }

    public void setDetailBean(PhotoDetailPageBean detailBean) {
        if (this.detailBean != null) {
            this.detailBean.close();
        }
        this.detailBean = detailBean;
    }

    public void close() {
        if (paginator != null) {
            paginator.close();
            paginator = null;
        }
        if (detailBean != null) {
            detailBean.close();
            detailBean = null;
        }
    }

    public void setPageSize(int pageSize) {
        getPaginator().setPageSize(pageSize);
        first();
    }

    public void selectAlbum(String albumName) {
        photos = paginator.setAlbumName(albumName);
    }

    public void removeSelected(String[] selectedPhotos) {
        if (selectedPhotos == null) {
            return;
        }
        int[] selectedIds = new int[selectedPhotos.length];
        for (int i = 0; i < selectedPhotos.length; i++) {
            String selectedPhoto = selectedPhotos[i];
            selectedIds[i] = Integer.parseInt(selectedPhoto);
        }

        albumDAO.removePhotos(selectedIds);
        first();
    }

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public void exportPhotos(String fromParam, String toParam) throws ParseException {
        Date fromDate = format.parse(fromParam);
        Date toDate = format.parse(toParam);
        exporter.exportPhotos(fromDate, toDate);
    }
}
